package com.android.model.goods;

import android.net.Uri;
import android.util.ArrayMap;
import android.util.Log;

import com.android.bean.BaseRecords;
import com.android.bean.Good;
import com.android.bean.GoodRecords;
import com.android.bean.GoodsType;
import com.android.bean.ResponseData;
import com.android.bean.User;
import com.android.bean.queryBean.UserGoodsRecords;
import com.android.model.login.LoginListener;
import com.android.utils.constraint.Constants;

import java.util.List;
import java.util.Map;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class GoodsModelimpl implements GoodsModel{

    private User user;
    private String TAG ="GoodsModelimpl";

    public GoodsModelimpl(User user) {
        this.user = user;
    }

    public GoodsModelimpl() {
    }

    @Override
    public void getAllGoodsType(GoodsListener goodsListener) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();

        GoodsHttp_interface httpInterface =
                retrofit.create(GoodsHttp_interface.class);
        Observable<ResponseData<List<GoodsType>>> observable
                = httpInterface.sendGetAllGoodsType();

        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData<List<GoodsType>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseData<List<GoodsType>> listResponseData) {
                        goodsListener.onGetAllGoodsType(listResponseData);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getAllGood(GoodsListener goodsListener) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();
        GoodsHttp_interface httpInterface =
                retrofit.create(GoodsHttp_interface.class);
        Map<String, Object> map = new ArrayMap<>();



        Observable<ResponseData<GoodRecords>> observable
                = httpInterface.sendGetAllGood(user.getId(), map);

        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData<GoodRecords>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseData<GoodRecords> listResponseData) {
                        goodsListener.onGetAllGoodSuccess(listResponseData);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("1111",e.toString());

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    @Override
    public void getUserRecords(Long userId, Map<String, Object> map,GoodsListener goodsListener) {
        Retrofit retrofit = new  Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        GoodsHttp_interface httpInterface = retrofit.create(GoodsHttp_interface.class);
        Observable<ResponseData<BaseRecords<UserGoodsRecords>>> observable
                = httpInterface.sendUserRecords(userId, map);

        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData<BaseRecords<UserGoodsRecords>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseData<BaseRecords<UserGoodsRecords>> baseRecordsResponseData) {
                        Log.e("1111111",baseRecordsResponseData.getMsg());
                        goodsListener.onGetUserRecordsSuccess(baseRecordsResponseData);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("获取用户交易记录",e.toString());

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


}
